package durst.data;

import java.sql.SQLException;

import org.apache.log4j.Logger;

import com.j256.ormlite.dao.ForeignCollection;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.field.ForeignCollectionField;
import com.j256.ormlite.table.DatabaseTable;
import com.sdicons.json.mapper.JSONMapper;

import durst.data.dao.DaoFactory;
import durst.data.dao.impl.FriendDao;
import durst.data.dao.impl.UserCalendarDao;
import durst.data.json.UserCalendarJSONHelper;

@DatabaseTable(tableName = "UserCalendar", daoClass = UserCalendarDao.class)
public class UserCalendar extends CSDEntity implements Comparable<UserCalendar> {
	public static final String OWNER_ID = "owner_id";
	public static final String DEF = "def";
	public static final String JUMPLIST = "jumplist";
	public static final String ID = "id";

	static {
		JSONMapper.addHelper(new UserCalendarJSONHelper());
	}
	
	public enum SharedStatus {
		PRIVATE, FRIENDS, PUBLIC
	}

	@DatabaseField(generatedId = true, columnName = ID)
	private int id;

	@DatabaseField(canBeNull = false)
	private String name;

	@DatabaseField(canBeNull = false)
	private SharedStatus shared;

	@DatabaseField(canBeNull = false, foreign = true, columnName = OWNER_ID)
	private Account owner;

	@DatabaseField(canBeNull = false, columnName = DEF)
	private boolean def;

	@DatabaseField(canBeNull = false, columnName = JUMPLIST)
	private boolean jumplist;
	
	@ForeignCollectionField
	private ForeignCollection<Event> events;

	public UserCalendar() {
	}

	public UserCalendar(String name, SharedStatus shared, Account owner,
			boolean def, boolean jumplist) {
		this.name = name;
		this.shared = shared;
		this.owner = owner;
		this.def = def;
		this.jumplist = jumplist;
	}

	@Override
	public int compareTo(UserCalendar that) {
		if (this.def && !that.def)
			return -1;
		else if (!this.def)
			return 1;
		return this.name.compareTo(that.name);
	}

	@Override
	public boolean equals(Object other) {
		return (other instanceof UserCalendar)
				&& this.id == ((UserCalendar) other).id;
	}

	@Override
	public int hashCode() {
		return id;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public SharedStatus getShared() {
		return shared;
	}

	public void setShared(SharedStatus shared) {
		this.shared = shared;
	}

	public ForeignCollection<Event> getEvents() {
		if (events == null)
			try {
				DaoFactory.getInstance().createDao(UserCalendar.class).refresh(this);
			}
			catch (SQLException e) {
				Logger.getLogger(Account.class).error("Count not auto refresh ForeignCollection");
			}
		return events;
	}

	public void setOwner(Account owner) {
		this.owner = owner;
	}

	public Account getOwner() {
		return owner;
	}

	public void setDef(boolean def) {
		this.def = def;
	}

	public boolean isDef() {
		return def;
	}

	@SuppressWarnings("unchecked")
	public boolean hasAccess(Account user) throws SQLException {
		if (shared == SharedStatus.FRIENDS && !owner.equals(user)) {
			FriendDao<Friend, Integer> friendDao = (FriendDao<Friend, Integer>) DaoFactory
					.getInstance().createDao(Friend.class);

			return friendDao.isFriendOf(user, owner);
		}
			
		return shared == SharedStatus.PUBLIC || owner.equals(user);
	}

	public void setJumplist(boolean jumplist) {
		this.jumplist = jumplist;
	}

	public boolean isJumplist() {
		return jumplist;
	}
}
